<?php
declare (strict_types = 1);

namespace app\model;

use think\facade\Config;
use think\facade\Db;
use think\Model;

/**
 * @mixin \think\Model
 */
class User extends Model
{
    public function checkUser($token,$param)
    {
        $res = checkToken($token);
        return $res;
    }

    public function loginUser($token,$param)
    {
        $member = Db::name("scale_user")
            ->where('username', $param['username'])
            ->where('password', $param['password'])
            ->where('token', $token);
        $member = $member->select();
        return $member;
    }

    public function createUser($param)
    {
        $data['username'] = $param['username'];
        $data['username'] = $param['username'];
        $userId = Db::name('user')->insertGetId($data);
        $userToken['token'] = signToken($userId);

    }

    public function resetPwd($uid,$newPassword)
    {
        $password = $this->encryptPassword($newPassword);
        $res = Db::name('scale_user')
            ->where('id', $uid)
            ->update(['password' => $password]);
        return $res;
    }

    private function encryptPassword($newPassword){
        $pwd_key = Config::get('app.pwd_key');
        return sha1($pwd_key.$newPassword);
    }
}
